from django.shortcuts import render
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import viewsets
from rest_framework.filters import SearchFilter, OrderingFilter
from .serializers import *
from .filters import *
from .models import Sale, GoodInSale, Purchase, GoodInPurchase, SaleComment, GoodComment, Refund
from apps.pagination import *


# Create your views here.
class PurchaseListViewSet(viewsets.ModelViewSet):
    queryset = Purchase.objects.all()  # queryset
    serializer_class = PurChaseWithGoodInPurchaseSerializer
    pagination_class = StandardResultsSetPagination
    filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter)
    filter_class = PurchaseFilter
    search_fields = []


class GoodInPurchaseListViewSet(viewsets.ModelViewSet):
    queryset = GoodInPurchase.objects.all()  # queryset
    serializer_class = GoodInPurchaseWithDetailedPurchaseSerializer
    pagination_class = StandardResultsSetPagination
    filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter)
    filter_class = GoodInPurchaseFilter
    search_fields = []


class SaleListViewSet(viewsets.ModelViewSet):

    """
    SaleListViewSet 多行注释.JPEG.
    　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　
    This viewset automatically provides `list`, `create`, `retrieve`,
    `update` and `destroy` actions.

    Additionally we also provide an extra `highlight` action.

    """

    queryset = Sale.objects.all()  # queryset
    serializer_class = SaleWithGoodInSaleSerializer
    pagination_class = StandardResultsSetPagination
    filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter)
    # fields和class二选一，如filter_fields = '__all__'和filter_class二选一
    filter_class = SaleFilter
    search_fields = []  # 正则 ^ = @ $
    # 配置了filter_backends但不配置fields则默认为'__all__'


class GoodInSaleListViewSet(viewsets.ModelViewSet):
    queryset = GoodInSale.objects.all()  # queryset
    serializer_class = GoodInSaleWithDetailedSaleSerializer
    pagination_class = StandardResultsSetPagination
    filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter)
    filter_class = GoodInSaleFilter
    search_fields = []


class SaleCommentListViewSet(viewsets.ModelViewSet):
    queryset = SaleComment.objects.all()
    serializers_class = SaleCommentSerializer
    pagination_class = StandardResultsSetPagination
    filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter)
    filter_class = SaleCommentFilter
    search_fields = []


class GoodCommentListViewSet(viewsets.ModelViewSet):
    queryset = GoodComment.objects.all()
    serializers_class = GoodCommentSerializer
    pagination_class = StandardResultsSetPagination
    filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter)
    filter_class = GoodCommentFilter
    search_fields = []


class RedundListViewSet(viewsets.ModelViewSet):
    queryset = Refund.objects.all()
    serializers_class = RefundSerializer
    pagination_class = StandardResultsSetPagination
    filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter)
    filter_class = RefundFilter
    search_fields = []






